//
//  YLAdressSettingViewController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/11.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLAdressSettingViewController.h"
#import "YLGlobeHeader.h"
#import "YLEditAdressViewController.h"
#import "YLMyAdressModel.h"
#import "YLAccountTool.h"
#import "WHNetworkTool.h"
#import "WHProgressHUD.h"
#import "UILabel+Extension.h"
#import "YLMyDefaultAddressViewModel.h"
@interface YLAdressSettingViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneNumberLabel;
@property (weak, nonatomic) IBOutlet UILabel *adressLabel;

@end

@implementation YLAdressSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"收货地址设置";
    
    self.scrollView.alwaysBounceVertical = YES;
    
    UIButton *managerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [managerButton setTitle:@"修改" forState:UIControlStateNormal];
    managerButton.titleLabel.font = [UIFont systemFontOfSize:13.0f];
    managerButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [managerButton setTitleColor:YLColor(94, 96, 107) forState:UIControlStateNormal];
    [managerButton addTarget:self action:@selector(toChange) forControlEvents:UIControlEventTouchUpInside];
    managerButton.bounds = CGRectMake(0, 0, 70, 30);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:managerButton];
    _nameLabel.text = _adressModel.receiveName;
    _phoneNumberLabel.text = _adressModel.receiveTel;
    _adressLabel.text = _adressModel.adress;
    [_adressLabel setLineSpacing:4];
    
}

- (void)toChange
{
    YLEditAdressViewController *edit = [[YLEditAdressViewController alloc] init];
    edit.adressModel = self.adressModel;
    edit.title = @"修改收货地址";
    [self.navigationController pushViewController:edit animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

// 删除地址
- (IBAction)deleteAdress:(UIButton *)sender {
    
    if (!_canDelete) {
        [WHProgressHUD showError:@"请至少保留一个收货地址"];
        return;
    }
    
    __weak typeof(self) weakSelf = self;
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定删除该地址吗？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSString *url = [NSString stringWithFormat:@"%@AppCommon/RemoveReceiveAddress", YLURL];
        
        NSDictionary *param = @{@"ReceiveAddressId" : @([weakSelf.adressModel.receiveAddressId integerValue])};
        
        [WHProgressHUD showStatus:@"正在删除地址,请勿退出"];
        [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:param success:^(id responseObject) {
            
            if ([responseObject[@"httpCode"] integerValue] == 200) {
               
                
                [[[[YLMyDefaultAddressViewModel alloc] init].requestCommand execute:nil] subscribeNext:^(id  _Nullable x) {
                     [WHProgressHUD showSuccess:@"删除成功"];
                     [weakSelf.navigationController popViewControllerAnimated:YES];
                }];
                
                if (weakSelf.deleteAddressBlock) {
                    weakSelf.deleteAddressBlock(weakSelf.index);
                }
                
                
                
            } else  {
                [WHProgressHUD showError:responseObject[@"message"]];
            }
            
            
        } fail:^(NSError *error) {
            
        }];
        
        
        
    }];
    
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
   
    [okAction setValue:YLMainColor forKey:@"_titleTextColor"];
    [cancleAction setValue:[UIColor blackColor] forKey:@"_titleTextColor"];
    
    [alertController addAction:cancleAction];
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}

- (void)dealloc
{
    [[WHNetworkTool shareInstance] cancel];
    [WHProgressHUD dismiss];
}



@end
